package com.wondersgroup.fjzlpt.org.controller.rest.jtqy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.quick.framework.util.ExcelOrWordUtils;
import com.quick.framework.util.QuickPrincipalUtils;
import com.wondersgroup.fjzlpt.org.service.inter.xymb.XymbService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.org.service.inter.jtqy.JtqyService;

@RestController
@RequestMapping("jtqy")
public class JtqyRestController {
	@Autowired
	private JtqyService jtqyService;
	@Autowired
	private XymbService xymbService;


	/**
     * 根据条件分页查询签约信息
     * @param request
     * @return
     */
	@RequestMapping("/queryPage")
	public JSONObject queryPage(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryPage(map);
	}
	
	
	
	/**
     * 查询签约分页数据
     * @param request
     * @return
     */
	@RequestMapping("/isExist")
	public JSONObject isExist(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("JGXZQHDM",QuickPrincipalUtils.getUserinfoByKey(request,"JGXZQHDM").toString());
		return jtqyService.isExist(map);
	}
	
    /**
     * 初始化签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/initQyxxData")
	public JSONObject initQyxxData(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.initQyxxData(map);
	}
	
	/**
     * 初始化签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/findTdByUserId")
	public JSONArray findTdByUserId(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.findTdByUserId(map);
	}
	
    /**
     * 保存签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/save")
	public JSONObject save(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.save(map);
	}
	
	/**
     * 根据主键删除签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/delete")
	public JSONObject delete(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.delete(map);
	}
	
	/**
     * 新增签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/addQy")
	public JSONObject addQy(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		//事件表
		map.put("YSID", map.get("USID"));
		map.put("YSXM", map.get("YFXM"));
		map.put("YLJGMC",map.get("YFYLJGMC"));
		map.put("PTJGID",map.get("YLJGID"));
		map.put("JGXZQHDM" , QuickPrincipalUtils.getXzqhdm(request));
		if(!StringUtils.isEmpty(map.remove("action"))){
			return jtqyService.xyfw(map);
		}

		return jtqyService.addQy(map);
	}
	
	
	/**
     * 续约签约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/xyfw")
	public JSONObject xyfw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.xyfw(map); 
	}
	
	/**
     * 批量续约信息数据
     * @param request
     * @return
     */
	@RequestMapping("/plxyfw")
	public JSONObject plxyfw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
        String USERID = QuickPrincipalUtils.getUserID(request);
        map.put("USERID", USERID);
		return jtqyService.plxyfw(map); 
	}
	
	/**
	 * 解约服务
	 * @param request
	 * @return
	 */
	@RequestMapping("/jyfw")
	public JSONObject jyfw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
        map.put("JYYH", QuickPrincipalUtils.getUserID(request));
		map.put("XM",(String)QuickPrincipalUtils.getUserinfoByKey(request,"XM"));
		map.put("YLJGMC",(String)QuickPrincipalUtils.getUserinfoByKey(request,"YLJGMC"));
		map.put("PTJGID",(String)QuickPrincipalUtils.getUserinfoByKey(request,"PTJGID"));

		return jtqyService.jyfw(map);
	}
	
	/**
	 * 家庭成员查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/jtcyQyxxQuery")
	public JSONObject jtcyQyxxQuery(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.jtcyQyxxQuery(map);
	}
	
	/**
	 * 变更服务包
	 * @param request
	 * @return
	 */
	@RequestMapping("/bgfwb")
	public JSONObject bgfwb(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("YSID", QuickPrincipalUtils.getUserID(request));
		map.put("YSXM",(String)QuickPrincipalUtils.getUserinfoByKey(request,"XM"));
		map.put("YLJGMC",(String)QuickPrincipalUtils.getUserinfoByKey(request,"YLJGMC"));
		map.put("PTJGID",QuickPrincipalUtils.getPtjgID(request));
		return jtqyService.bgfwb(map);
	}
	
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		JSONArray jsarry = jtqyService.queryExportList(map);
		//JSONArray jsarry = JSONArray.parseArray(map.get("exportData"));
		JSONObject json = new JSONObject();
		json.put("XM", "姓名");
		json.put("ZJHM", "证件号码");
		json.put("SJHM", "手机号码");
		json.put("JTZZ", "家庭住址");
		json.put("QYSJ", "签约时间");
		json.put("RQFLMC", "人群分类");
		json.put("TDMC", "团队名称");
		json.put("FWBMC", "签约服务包");
		json.put("QYZT", "签约状态");
		jsarry.add(0, json);
		
		
		List<String> titleField = new ArrayList<String>();
		titleField.add("XM");
		titleField.add("ZJHM");
		titleField.add("SJHM");
		titleField.add("JTZZ");
		titleField.add("QYSJ");
		titleField.add("RQFLMC");
		titleField.add("TDMC");
		titleField.add("FWBMC");
		titleField.add("QYZT");
		ExcelOrWordUtils.downLoadExcelFile(jsarry, titleField,"正式签约列表",request, response);
	}
	
	/**
	 * 查询正常可用的团队
	 * @param request
	 * @return
	 */
	@RequestMapping("queryNormalTdxx")
	public JSONObject queryNormalTdxx(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryNormalTdxx(map);
	}
	
	/**
	 * 保存转介信息
	 * @param request
	 * @return
	 */
	@RequestMapping("saveZjxx")
	public JSONObject saveZjxx(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.saveZjxx(map);
	}
	
	/**
	 * 分页查询待审核的转介申请
	 * @param request
	 * @return
	 */
	@RequestMapping("queryZjsqByPage")
	public JSONObject queryZjsqByPage(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryZjsqByPage(map);
	}
	
	/**
	 * 批量审核转介申请
	 * @param request
	 * @return
	 */
	@RequestMapping("checkZjsq")
	public JSONObject checkZjsq(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("USERID",QuickPrincipalUtils.getUserID(request));
		map.put("XM",(String)QuickPrincipalUtils.getUserinfoByKey(request,"XM"));
		map.put("YLJGMC",(String)QuickPrincipalUtils.getUserinfoByKey(request,"YLJGMC"));
		map.put("PTJGID",QuickPrincipalUtils.getPtjgID(request));
		return jtqyService.checkZjsq(map);
	}
	
	/**
	 * 查询一个居民的所有历史签约信息
	 * @param request
	 * @return
	 */
	@RequestMapping("querySingleUserQyxxAll")
	public JSONObject querySingleUserQyxxByPage(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.querySingleUserQyxxAll(map);
	}
	
	/**
	 * 导出转介审核数据
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadZjsh")
	public void downloadZjsh(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		jtqyService.downloadZjsh(map,request,response);
	}
	
	
	/**
	 * 导出签约查询页面数据
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadQycx")
	public void downloadQycx(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		jtqyService.downloadQycx(map,request,response);
	}
	
	/**
	 * 打印协议
	 * @param request
	 * @param response
	 */
//	@RequestMapping("printXy")
//	public void printXy(HttpServletRequest request,HttpServletResponse response){
//		Map<String, String> map = RequestUtils.GetParameterMap(request);
//		jtqyService.printXy(map, response);
//	}

//	/**
//	 * 下载word文件的协议服务包
//	 * @param request
//	 * @param response
//	 */
//	@RequestMapping("downXyFwbByWord")
//	public void downXyFwbByWord(HttpServletRequest request,HttpServletResponse response){
//		JSONArray fwbArr = jtqyService.queryAllByView(null);
//		Map<String,Object> dataMap = new HashMap<String,Object>();
//		dataMap.put("list",fwbArr);
//		ExcelOrWordUtils.downLoadWordFile(dataMap,response,"printXyFwb.ftl","/config/wordModel");
//	}

	/**
	 * 查询协议服务包列表--用于打印或查询家签协议服务包价格
	 * @param request
	 */
	@RequestMapping("queryXyFWB")
	public JSONArray downXyFwbByWord(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryFwbAllByView(map);
	}

	/**
	 * 审核预签约记录（同意签约/拒绝签约）
	 * @param request
	 * @return
	 */
	@RequestMapping("checkYqyData")
	public JSONObject checkYqyData(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		if(StringUtils.isEmpty(map.get("USID"))){ //拒绝预签约前台不会传USID
			map.put("USID", QuickPrincipalUtils.getUserID(request));
		}
		map.put("PTJGID", QuickPrincipalUtils.getPtjgID(request));
		map.put("YLJGMC", QuickPrincipalUtils.getYljgmc(request));
		return jtqyService.checkYqyData(map);
	}

	/**
	 * 分页查询电子协议
	 * @param request
	 * @return
	 */
	@RequestMapping("queryDzxyByage")
	public JSONObject updateXyjlQyzt(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryDzxyByage(map);
	}

	/**
	 * 根据协议ID查询协议
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryXyByXyid")
	public JSONObject queryXymb(HttpServletRequest request){
		String XYID = request.getParameter("XYID");
		return jtqyService.queryXyByXyid(XYID);
	}

	/**
	 * 查询正在使用中的家庭签约协议
	 * @return
	 */
	@RequestMapping("/queryUseJtqyxy")
	public JSONObject queryUseJtqyxy(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("JGXZQHDM",(String)QuickPrincipalUtils.getUserinfoByKey(request,"JGXZQHDM"));
		return xymbService.queryUseJtqyxy(map);
	}

	/**
	 * 查询患者已经签订的所有服务包ID
	 * 	--用于前台服务包禁用
	 * @return
	 */
	@RequestMapping("/getYqdFwbid")
	public JSONObject getYqdFwbid(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.getYqdFwbid(map);
	}

	/**
	 * 查询医保服务项
	 * @return
	 */
	@RequestMapping("/queryYbfwx")
	public JSONArray queryYbfwx(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return jtqyService.queryYbfwx(map);
	}

}
